var browser_base = (function (global){
    let document = global?.document;
    if (!document) return console.error('缺少 document 对象！');

    /**环境变量。连续点击页面打开配置界面
     * @param {String} key 配置名
     * @param {*} [def] 配置不存在时，返回的默认值
     */
    function s94_env(key, def){
        if (!s94_env.data){
            s94_env.data = {};
            (global.localStorage.getItem(s94_env.CACHE_KEY) || 'debug=0').split(/\n+/).forEach(function (row){
                row = row.replace(/^\s+|\s+$/g,'');
                if (/^(\#|\/{2})/.test(row)) return;
                let arr = row.split(/\s*=\s*/);
                let k = arr.shift().toLowerCase();
                s94_env.data[k] = arr.join('=');
            })
        }
        key = key.toLowerCase();
        return typeof(s94_env.data[key]) === 'undefined' ? def : s94_env.data[key];
    }
    s94_env.CACHE_KEY = 'cache_s94_env';
    s94_env.init = function (){
        let num=0, timeout_id, max = 10;
        //根节点绑定事件用于触发，如果要避免，可以在html节点阻止click事件冒泡
        document.addEventListener('click', ()=>{
            clearTimeout(timeout_id);
            timeout_id = setTimeout(()=>{num=0}, 400);
            num++;
            if (num >= max && confirm('是否打开配置界面')) {
                s94_env.open();
            }
        })
    }
    s94_env.open = function(){
        if (!s94_env.outer){
            let content = global.localStorage.getItem(s94_env.CACHE_KEY) || 'debug=0';
            s94_env.outer = document.createElement('form');
            s94_env.outer.style = 'position: fixed;display: flex;flex-direction: column;z-index: 9999;left: 0;top: 0;bottom: 0;right: 0;background: #fff;padding: 3.5em 1em 2.5em;';
            s94_env.outer.innerHTML = `
<span style="position: absolute;top: 2em;right: 1em;cursor: pointer;" onclick="this.parentNode.style.display='none'">关闭</span>
<textarea name="content" style="width: 100%;flex-grow: 1;box-sizing: border-box;">${content}</textarea>
<div style="font-size: 0.8em;color: #f56c6c">格式：配置名=值。注意：1、每行为一个配置，以#或者//开头表示注释；2、所有配置的值都为字符串</div>
<div style="display: flex;justify-content: space-between;padding-top: 1em;">
  <input type="button" onclick="" value="清理缓存">
  <input type="submit" value="提交">
</div>`;
            document.querySelector('body').appendChild(s94_env.outer);
            let dom_content = s94_env.outer.querySelector('textarea');
            s94_env.outer.querySelector('input[type="button"]').addEventListener('click', function (){
                if (confirm('是否确定清除本地缓存的数据')){
                    global.localStorage.clear();
                    global.localStorage.setItem(s94_env.CACHE_KEY, dom_content.value);
                }
            })
            s94_env.outer.addEventListener('submit', function (e){
                e.stopPropagation();
                e.preventDefault();
                global.localStorage.setItem(s94_env.CACHE_KEY, dom_content.value);
                s94_env.data = false;
                s94_env.outer.style.display = 'none';
            })
        }else {
            s94_env.outer.style.display = 'flex';
        }
    }
    s94_env.init();

    /**缓存加载js、css文件
     * @param {String[]|String} src_arr 文件地址
     */
    function cache_load(src_arr){
        if (typeof src_arr === 'string') src_arr = [src_arr];
        let loading = 0, code_list = [];
        src_arr.forEach(function (src){
            let cache_name = 'cache_load-'+src;
            let code = global.localStorage.getItem(cache_name);
            if (code) {
                code_list.push({code:code, src:src})
            }else {
                loading++;
                let xhr = new XMLHttpRequest();
                xhr.open('get', src);
                xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                xhr.onload = function (){
                    global.localStorage.setItem(cache_name, xhr.responseText);
                    if (--loading <= 0) global.location.reload();
                }
                xhr.send();
            }
        })
        if (loading === 0) code_list.forEach(function (row){action(row.code, row.src)})
        function action(code, src){
            let type = src.match(/\.(js|css)(\?|$)/i)?.[1];
            switch (type) {
                case 'css':{
                    let dom = document.createElement('style');
                    dom.innerHTML = code;
                    document.querySelector('head').appendChild(dom);
                }break;
                case 'js':{
                    let dom = document.createElement('script');
                    if (/(^|;|\s)(import|export)\s+/.test(code)) dom.type = 'module';
                    dom.innerHTML = code;
                    document.querySelector('head').appendChild(dom);
                }break;
            }
        }
    }

    global.s94_env = s94_env;
    global.cache_load = cache_load;

    return {s94_env,cache_load};
})(typeof globalThis !== 'undefined' ? globalThis :
    typeof self !== "undefined" ? self :
        typeof window !== "undefined" ? window :
            typeof global !== "undefined" ? global : {})
